package com.teamelite.exmgmt;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.ImageView;

public class cSummaryDetail extends Activity implements OnClickListener {
	private String year,month,id_pengguna;
	@Override	
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detailsummary);
		
		TableLayout tl = (TableLayout)findViewById(R.id.tabla_cuerpo);
		
		Double TCalories[] = {};
		String AType[] = {};
		long hr[] = {};
		long min[] = {};
		long sec[] = {};
		String lastActivity;
		int i=0;
		
		this.year=getIntent().getExtras().getString("year");
		this.month=getIntent().getExtras().getString("month");
		this.id_pengguna=getIntent().getExtras().getString("id_pengguna");
		
		DBHelper db = new DBHelper(this);
		int dataCount = db.getCountActivitySumDetail(this.id_pengguna,this.year,this.month);
		TCalories = new Double[dataCount+1];
		AType = new String[dataCount+1];
		hr=new long[dataCount+1];
		min=new long[dataCount+1];
		sec=new long[dataCount+1];
		Cursor cur = db.getActivitySumDetail(this.id_pengguna,this.year,this.month);
		if (cur.moveToFirst()){
			lastActivity = cur.getString(3);
			i++;
			AType[i] = cur.getString(7);
			TCalories[i] = 0.0;
			hr[i]=min[i]=sec[i]=0;
			do{
				if (cur.getString(3).equals(lastActivity)){
					TCalories[i] = TCalories[i]+cur.getDouble(6);
					// Set Duration Total
					String[] splitTmp = cur.getString(4).split(":");
					hr[i] = hr[i]+Integer.parseInt(splitTmp[0]);
					min[i] = min[i]+Integer.parseInt(splitTmp[1]);
					sec[i] = sec[i]+Integer.parseInt(splitTmp[2]);
				}else{
					lastActivity = cur.getString(3);
					i++;
					AType[i] = cur.getString(7);
					TCalories[i] = 0.0;
					hr[i]=min[i]=sec[i]=0;
					//
					TCalories[i] = TCalories[i]+cur.getDouble(6);
					// Set Duration Total
					String[] splitTmp = cur.getString(4).split(":");
					hr[i] = hr[i]+Integer.parseInt(splitTmp[0]);
					min[i] = min[i]+Integer.parseInt(splitTmp[1]);
					sec[i] = sec[i]+Integer.parseInt(splitTmp[2]);
				}
			}while(cur.moveToNext());
		}
		long tmp_hr,tmp_min,tmp_sec;
		
		for(int j=1;j<=i;j++){
			tmp_sec=tmp_min=tmp_hr=0;
			tmp_sec = sec[j];
			tmp_min = (min[j]+(tmp_sec/60));
			tmp_hr = (hr[j]+(tmp_min/60));
			this.setupView(j,tl,AType[j],String.format("%02d:%02d:%02d", tmp_hr,tmp_min%60,tmp_sec%60),TCalories[j]);
		}
		
		((ImageView)findViewById(R.id.imgCancel)).setOnClickListener(this);
		
		cur.close();
		db.close();
		
	}
	
	private void setupView(int id,TableLayout tl, String ActivityType, String TDuration, Double TCalories){
		TableRow row;
		TextView label;
		
		row = new TableRow(this);
		row.setId(10+id);
		row.setLayoutParams(new LayoutParams(
		LayoutParams.MATCH_PARENT,
		LayoutParams.WRAP_CONTENT));
		
		label = new TextView(this);
		label.setId(20+id);
		label.setBackgroundResource(R.drawable.tabel_shape);
		label.setText(ActivityType);
		label.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL);
		label.setTextColor(Color.parseColor("#FFFF00"));
		label.setTextAppearance(this, android.R.attr.textAppearanceMedium);
		label.setTextAppearance(this, R.style.boldText);
		label.setLayoutParams(new TableRow.LayoutParams(
               LayoutParams.WRAP_CONTENT,
               getDp(40f),
               0.2f));
        row.addView(label);
        
        tl.addView(row);
        
        // Row 2
		row = new TableRow(this);
		row.setId(11+id);
		row.setLayoutParams(new LayoutParams(
		LayoutParams.MATCH_PARENT,
		LayoutParams.WRAP_CONTENT));
		
		label = new TextView(this);
		label.setId(21+id);
		label.setBackgroundResource(R.drawable.tabel_shape);
		label.setText("Total Duration");
		label.setGravity(Gravity.CENTER_VERTICAL);
		label.setTextColor(Color.parseColor("#FFFF00"));
		label.setTextAppearance(this, android.R.attr.textAppearanceMedium);
		label.setTextAppearance(this, R.style.boldText);
		label.setPadding(getDp(5f), getDp(5f), getDp(5f), getDp(5f));
		label.setLayoutParams(new TableRow.LayoutParams(
               LayoutParams.WRAP_CONTENT,
               getDp(40f),
               0.2f));
        row.addView(label);
        
        label = new TextView(this);
        label.setId(22+id);
        label.setBackgroundResource(R.drawable.tabel_shape);
        label.setText(""+TDuration);
        label.setGravity(Gravity.CENTER_VERTICAL);
        label.setTextAppearance(this, android.R.attr.textAppearanceMedium);
		label.setTextAppearance(this, R.style.boldText);
        label.setPadding(getDp(5f), getDp(5f), getDp(5f), getDp(5f));
        label.setLayoutParams(new TableRow.LayoutParams(
               LayoutParams.WRAP_CONTENT,
               getDp(40f),
               0.2f));
        row.addView(label);
        
        tl.addView(row);
        
        // Row 3
   		row = new TableRow(this);
   		row.setId(12+id);
   		row.setLayoutParams(new LayoutParams(
   		LayoutParams.MATCH_PARENT,
   		LayoutParams.WRAP_CONTENT));
     		
   		label = new TextView(this);
   		label.setId(23+id);
   		label.setBackgroundResource(R.drawable.tabel_shape);
   		label.setText("Total Calories Burned");
   		label.setGravity(Gravity.CENTER_VERTICAL);
   		label.setTextColor(Color.parseColor("#FFFF00"));
   		label.setTextAppearance(this, android.R.attr.textAppearanceMedium);
   		label.setTextAppearance(this, R.style.boldText);
   		label.setPadding(getDp(5f), getDp(5f), getDp(5f), getDp(5f));
   		label.setLayoutParams(new TableRow.LayoutParams(
              LayoutParams.WRAP_CONTENT,
              getDp(40f),
              0.2f));
        row.addView(label);
             
        label = new TextView(this);
        label.setId(24+id);
        label.setBackgroundResource(R.drawable.tabel_shape);
        label.setText(String.format("%.2f cal", TCalories));
        label.setGravity(Gravity.CENTER_VERTICAL);
        label.setTextAppearance(this, android.R.attr.textAppearanceMedium);
        label.setTextAppearance(this, R.style.boldText);
        label.setPadding(getDp(5f), getDp(5f), getDp(5f), getDp(5f));
        label.setLayoutParams(new TableRow.LayoutParams(
              LayoutParams.WRAP_CONTENT,
              getDp(40f),
              0.2f));
        row.addView(label);
             
        tl.addView(row);
        
        // Row 4
   		row = new TableRow(this);
   		row.setId(13+id);
   		row.setLayoutParams(new LayoutParams(
   		LayoutParams.MATCH_PARENT,
   		LayoutParams.WRAP_CONTENT));
     		
   		label = new TextView(this);
   		label.setId(25+id);
   		label.setBackgroundResource(R.drawable.cell_shape);
   		label.setPadding(getDp(5f), getDp(5f), getDp(5f), getDp(5f));
   		label.setLayoutParams(new TableRow.LayoutParams(
              LayoutParams.WRAP_CONTENT,
              getDp(10f),
              0.2f));
        row.addView(label);
             
        tl.addView(row);

	}
	
	protected int getDp(float dp){
		DisplayMetrics metrics = this.getResources().getDisplayMetrics();
        int pixels = (int) (metrics.density * dp + 0.5f);
		return pixels;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.imgCancel:
			finish();
		break;
		}
	}
}
